package com.sgb.kjwl.view.ui.activity.self.pick;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import com.google.android.material.tabs.TabLayout;
import androidx.fragment.app.Fragment;

import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivitySelfPickList4Binding;
import com.sgb.kjwl.di.pick.DaggerPickComponent;
import com.sgb.kjwl.di.pick.PickModule;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.jicai.event.JiCaiEventEntity;
import com.swgk.core.event.EventConstant;
import com.sgb.link.view.adapter.PickNoticeAdapter;
import com.sgb.kjwl.view.ui.fragment.self.pick.SelfPickListFragment4;
import com.sgb.kjwl.viewmodel.pick.SelfPickList4Model;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.recyclerview.adapter.SlidingFragmentPagerAdapter;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

//个人中心——集采、我的集采、我的加盟
public class SelfPickListActivity4 extends AppActivity {

    @Inject
    SelfPickList4Model model;
    private ActivitySelfPickList4Binding binding;
    private PickNoticeAdapter pagerAdapter;
    private ArrayList<Fragment> mFragments;
    private InputMethodManager im;
    private ArrayList<String> titles = new ArrayList<>();
    //类别：1.我的集采、  2.我的加盟
    private int type;

    public static void start(Context context, int type) {
        Intent intent = new Intent(context, SelfPickListActivity4.class);
        intent.putExtra("TYPE", type);
        context.startActivity(intent);
    }


    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerPickComponent.builder()
                .appComponent(appComponent)
                .pickModule(new PickModule(this))
                .build()
                .inject(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_self_pick_list4);
        binding.ivBack.setOnClickListener(this::onMultiClick);
        binding.tvSearch.setOnClickListener(this::onMultiClick);
        binding.tvSearchDelete.setOnClickListener(this::onMultiClick);
        binding.etView.setHint("搜索商品名称或集采地");
//        binding.setViewModel();
        type = getIntent().getIntExtra("TYPE", 0);
        switch (type) {
            case 1:
//                binding.tvTitle.setText("我的集采");
                binding.tvTitle.setText("需方发起");
                break;
            case 2:
                binding.tvTitle.setText("我的加盟");
                break;
        }
        im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    }

    @Override
    protected void init() {
        //使用动态获取的分类
        model.groupBuyClassList();
        binding.etView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
//                    if (TextUtils.isEmpty(binding.etView.getText().toString())) {
//                        MToast.showToast(SelfPickListActivity4.this, "请输入关键字");
//                        return true;
//                    }
                    //调用Fragment的刷新
                    EventBus.getDefault().post(new JiCaiEventEntity(EventConstant.EVENT_JICAI_MY_CEN_AND_JOIN, binding.etView.getText().toString().trim()));
                    hideKeyboard();
                }
                return false;
            }
        });
    }

    public void updateViews(List<PickSelectEntity> data) {
        if (data == null || data.size() == 0) {
            DialogHelper.dismissProgressDialog();
            showEmpty();
            return;
        }
        String[] mTitles = new String[data.size()];
        ArrayList<Fragment> mFragments = new ArrayList<>();
        for (int i = 0; i < data.size(); i++) {//类型：商品集采、机械集采、设备集采，劳务…………
            mTitles[i] = data.get(i).getProjectscale();
            mFragments.add(SelfPickListFragment4.newInstance(type, data.get(i).getNo().isEmpty()?0:Integer.valueOf(data.get(i).getNo()), 0));
        }
        SlidingFragmentPagerAdapter mPagerAdapter = new SlidingFragmentPagerAdapter(getSupportFragmentManager(), mFragments, mTitles);
        binding.viewpager.setOffscreenPageLimit(mTitles.length);
        binding.viewpager.setAdapter(mPagerAdapter);
        binding.tabLayout.setupWithViewPager(binding.viewpager);
        binding.tabLayout.setTabMode(data.size() > 4 ? TabLayout.MODE_SCROLLABLE : TabLayout.MODE_FIXED);
    }

    public void showEmpty() {
//        binding.emptyView.setVisibility(View.VISIBLE);
    }

    @Override
    public void onMultiClick(View v) {
        super.onMultiClick(v);
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_search:

                break;
            case R.id.tv_search_delete:
                binding.etView.setText("");
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        hideKeyboard();
    }

    /**
     * 获取InputMethodManager，隐藏软键盘
     */
    private void hideKeyboard() {
        InputMethodManager imm = (InputMethodManager) this.getSystemService(Context.INPUT_METHOD_SERVICE);
        // 隐藏软键盘
        imm.hideSoftInputFromWindow(this.getWindow().getDecorView().getWindowToken(), 0);
    }

}
